import ISendMsg from "../../form/ISendMsg";
import { doReLaunch } from "../../util/CommonUtil";

Page({
  data: {
    url: '',
    resCode: ''
  },
  onLoad(options) {
    const data: ISendMsg = JSON.parse(options.data || '{}');
    if (!data.code) {
      console.error('webView：code 为空');
      return // 结束方法
    }

    this.setData({
      url: data.url
    })

    if (data.code === 1) {
      // 1 登录
      this.login()
    }
  },
  login() {
    const that = this
    wx.login({
      success(res) {
        if (res.code) {
          that.setData({
            resCode: res.code
          }, () => {
            that.reLaunch()
          })
        } else {
          console.error('webView：登录失败！' + res.errMsg)
        }
      }
    })
  },
  reLaunch() {
    doReLaunch(this.data.url + '?code=' + this.data.resCode)
  }
})